package com.kinyx.framework.log;

import com.kinyx.framework.utils.KDateTimeUtils;

/**
 * 日志（控制台实现类）
 */
final class Log4Console extends Log {

	/** 类名 */
	private final String className;

	/** trace级别是否可用 */
	private boolean isTraceEnabled = false;
	/** debug级别是否可用 */
	private boolean isDebugEnabled = false;
	/** info级别是否可用 */
	private boolean isInfoEnabled = false;
	/** warn级别是否可用 */
	private boolean isWarnEnabled = false;
	/** error级别是否可用 */
	private boolean isErrorEnabled = false;
	/** fatal级别是否可用 */
	private boolean isFatalEnabled = false;

	private void initLogLevelEnabled(final LogLevel level) {
		switch (level) {
			case trace:
				this.isTraceEnabled = true;
			case debug:
				this.isDebugEnabled = true;
			case info:
				this.isInfoEnabled = true;
			case warn:
				this.isWarnEnabled = true;
			case error:
				this.isErrorEnabled = true;
			case fatal:
				this.isFatalEnabled = true;
		}
	}

	/**
	 * 打印输出
	 * @param level 日志级别
	 * @param message 内容信息
	 * @param t 异常信息
	 */
	private void println(final LogLevel level, final Object message, final Throwable t) {
		switch (level) {
			case trace:
				if (!this.isTraceEnabled) { return; }
				break;
			case debug:
				if (!this.isDebugEnabled) { return; }
				break;
			case info:
				if (!this.isInfoEnabled) { return; }
				break;
			case warn:
				if (!this.isWarnEnabled) { return; }
				break;
			case error:
				if (!this.isErrorEnabled) { return; }
				break;
			case fatal:
				if (!this.isFatalEnabled) { return; }
				break;
		}

		System.out.println(String.format("[%s] %s [%s] => %s", level.getValue(), KDateTimeUtils.now("yyyy-MM-dd HH:mm:ss,SSS"), this.className, String.valueOf(message)));
		if (t != null) {
			t.printStackTrace();
		}
	}

	/**
	 * 构造方法
	 * @param clazz 输出日志所在的类类型
	 */
	protected Log4Console(final Class<?> clazz) {
		super(clazz);
		this.className = clazz.getName();
		// 初始化日志级别
		this.initLogLevelEnabled(Logger.getLevel(this.className));
	}

	@Override
	public boolean isTraceEnabled() { return this.isTraceEnabled; }
	@Override
	public boolean isDebugEnabled() { return this.isDebugEnabled; }
	@Override
	public boolean isInfoEnabled() { return this.isInfoEnabled; }
	@Override
	public boolean isWarnEnabled() { return this.isWarnEnabled; }
	@Override
	public boolean isErrorEnabled() { return this.isErrorEnabled; }
	@Override
	public boolean isFatalEnabled() { return this.isFatalEnabled; }

	@Override
	public void trace(final Object message) { this.println(LogLevel.trace, message, null); }
	@Override
	public void debug(final Object message) { this.println(LogLevel.debug, message, null); }
	@Override
	public void info(final Object message) { this.println(LogLevel.info, message, null); }
	@Override
	public void warn(final Object message) { this.println(LogLevel.warn, message, null); }
	@Override
	public void error(final Object message) { this.println(LogLevel.error, message, null); }
	@Override
	public void fatal(final Object message) { this.println(LogLevel.fatal, message, null); }

	@Override
	public void trace(final Object message, final Throwable t) { this.println(LogLevel.trace, message, t); }
	@Override
	public void debug(final Object message, final Throwable t) { this.println(LogLevel.debug, message, t); }
	@Override
	public void info(final Object message, final Throwable t) { this.println(LogLevel.info, message, t); }
	@Override
	public void warn(final Object message, final Throwable t) { this.println(LogLevel.warn, message, t); }
	@Override
	public void error(final Object message, final Throwable t) { this.println(LogLevel.error, message, t); }
	@Override
	public void fatal(final Object message, final Throwable t) { this.println(LogLevel.fatal, message, t); }

}
